package com.landeb.shop.huo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.landeb.shop.R;
import com.landeb.shop.huo.bean.BeanSearchResultAct;

import java.util.List;

/**
 * Created by blade on 2016/10/24.
 */

public class ActSearchResultRVAdapter extends RecyclerView.Adapter<ActSearchResultRVAdapter.MyHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<BeanSearchResultAct.DataBean.ListBean> mList;


    private OnItemClickListener mOnItemClickListener;


    public ActSearchResultRVAdapter(Context context, List<BeanSearchResultAct.DataBean.ListBean> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        MyHolder holder = new MyHolder(mInflater.inflate(R.layout.item_act_search_result_recycler_view, parent, false));

        return holder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {
        // TODO: 2016/10/24 设置图片  按钮事件

        Glide.with(mContext)
                .load(mList.get(position).getPic())
                .placeholder(R.drawable.loading_pic)
                .error(R.drawable.error_pic)
                .into(holder.mImg);
//        holder.mButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(mContext, v.getId() + "", Toast.LENGTH_SHORT).show();
//            }
//        });


        holder.mTitle.setText(mList.get(position).getName());
        holder.mPrice.setText("¥" + mList.get(position).getPrice());
        holder.mComment.setText(mList.get(position).getComment() + "条评价");
        holder.mApplauseRate.setText(mList.get(position).getComent_good_percent() + "%好评");


        if (mOnItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                }
            });

        }

    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private ImageView mImg;
        private TextView mTitle;
        private TextView mPrice;
        private TextView mComment;
        private TextView mApplauseRate;

        public MyHolder(View itemView) {
            super(itemView);

            mImg = (ImageView) itemView.findViewById(R.id.iv_item_act_search_result_recyclerView);
            mTitle = (TextView) itemView.findViewById(R.id.tv_title_item_act_search_result_recyclerView);
            mPrice = (TextView) itemView.findViewById(R.id.tv_price_item_act_search_result_recyclerView);
            mComment = (TextView) itemView.findViewById(R.id.tv_comment_item_act_search_result_recyclerView);
            mApplauseRate = (TextView) itemView.findViewById(R.id.tv_applauseRate_item_act_search_result_recyclerView);

        }
    }

}
